package com.mall.controller;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class BaseController<T> {

    @InitBinder
    public void initBinder(ServletRequestDataBinder binder){

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        /**
         * 通过重新构造客户端内容，
         * 寻找与实体中Date.class属性名和参数名称相同的数据，在通过sdf的方式进行转换为Date
         */
        binder.registerCustomEditor(Date.class,new CustomDateEditor(sdf,true));
    }

    @RequestMapping("/find")
    public Map<String,Object> find(T t){return null;}

    @RequestMapping("/get")
    public T get(T t){return null;}

    @RequestMapping("/add")
    public Map<String,Object> add(T t){return null;}

    @RequestMapping("/update")
    public Map<String,Object> update(T t){return null;}

    @RequestMapping("/delete")
    public Map<String,Object> delete(T t){return null;}


    public Map<String,Object> findMap(List<T> list,long count){
        Map<String,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("data",list);
        map.put("count",count);
        //为了日志
        map.put("is",true);
        map.put("msg","查询成功");
        return map;
    }

    public Map<String,Object> iud(String msg,boolean b,Object o){
        Map<String,Object> map = new HashMap<>();
        map.put("is",b);
        map.put("msg",msg+(b?"成功":"失败"));
        map.put("obj",o);
        return map;
    }
}
